package hjslm.Controller.Background;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import hjslm.Bean.ShijuanBean;
import hjslm.Service.IBaseTwoService;
import hjslm.Service.IShijuanService;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import hjslm.Bean.BaseTwoBean;
import hjslm.Common.Impl.CommonConstant;
import hjslm.Common.Impl.CommonFunImpl;
import hjslm.Common.Impl.OSinfo;
import framework.base.common.Pager;
import framework.base.controller.BaseController;

@Controller
@RequestMapping("/b_shijuan")
public class ShijuanController extends BaseController{

	/**
	 * 试卷控制层
	 */
	private static final long serialVersionUID = 1L;
	
	@Autowired
	IShijuanService shijuanService;
	
	@Autowired
	IBaseTwoService basetwoService;

	@Override
	public void setLogger() {
	}
	
	@RequestMapping("/index")
	public ModelAndView index(Pager<ShijuanBean> pager){
		ModelAndView modelAndView = new ModelAndView("background/shijuan/index");
		pager = shijuanService.selectPage(null, pager);
		modelAndView.addObject("pager", pager);
		return modelAndView;
	}
	
	@RequestMapping("/add")
	public ModelAndView add(){
		ModelAndView modelAndView = new ModelAndView("background/shijuan/add");
		
		//基本信息
		List<BaseTwoBean> listbt = basetwoService.selectList(null);
		List<BaseTwoBean> listGrade = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listSubject = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listNianFen = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listLeixing = new ArrayList<BaseTwoBean>();
		for (BaseTwoBean baseTwoBean : listbt) {
			switch (baseTwoBean.getBotype()) {
			case "试卷年级":
				listGrade.add(baseTwoBean);
				break;
			case "试卷科目":
				listSubject.add(baseTwoBean);
				break;
			case "试卷年份":
				listNianFen.add(baseTwoBean);
				break;
			case "试卷类型":
				listLeixing.add(baseTwoBean);
				break;
			}
		}
		modelAndView.addObject("listGrade", listGrade);
		modelAndView.addObject("listSubject", listSubject);
		modelAndView.addObject("listNianFen", listNianFen);
		modelAndView.addObject("listLeixing", listLeixing);
		return modelAndView;
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(ShijuanBean bean){
		ModelAndView modelAndView = new ModelAndView("background/shijuan/edit");
		bean = shijuanService.selectOne(bean);
		modelAndView.addObject("bean", bean);
		
		//基本信息
		List<BaseTwoBean> listbt = basetwoService.selectList(null);
		List<BaseTwoBean> listGrade = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listSubject = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listNianFen = new ArrayList<BaseTwoBean>();
		List<BaseTwoBean> listLeixing = new ArrayList<BaseTwoBean>();
		for (BaseTwoBean baseTwoBean : listbt) {
			switch (baseTwoBean.getBotype()) {
			case "试卷年级":
				listGrade.add(baseTwoBean);
				break;
			case "试卷科目":
				listSubject.add(baseTwoBean);
				break;
			case "试卷年份":
				listNianFen.add(baseTwoBean);
				break;
			case "试卷类型":
				listLeixing.add(baseTwoBean);
				break;
			}
		}
		modelAndView.addObject("listGrade", listGrade);
		modelAndView.addObject("listSubject", listSubject);
		modelAndView.addObject("listNianFen", listNianFen);
		modelAndView.addObject("listLeixing", listLeixing);
		
		return modelAndView;
	}
	
	@RequestMapping("/insert")
	public void insert(ShijuanBean bean,HttpServletResponse response){
		//设置试卷属性
		bean.setId(CommonFunImpl.uuid("sj"));
		bean.setInserttime(CommonFunImpl.currentTime(1));
		int retVal = shijuanService.insert(bean);
		// 返回值
		try {
			PrintWriter out = response.getWriter();
			out.print(retVal);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();// 打印异常
		}
	}
	
	@RequestMapping("/update")
	public void update(ShijuanBean bean,HttpServletResponse response){
		//设置试卷属性
		bean.setInserttime(CommonFunImpl.currentTime(1));
		int retVal = shijuanService.update(bean);
		// 如果插入成功
		try {
			PrintWriter out = response.getWriter();
			out.print(retVal);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();// 打印异常
		}
	}

	@RequestMapping("/delete")
	public void delete(ShijuanBean bean, HttpServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		int ret = shijuanService.delete(bean);
		try {
			PrintWriter out = response.getWriter();
			out.print(ret);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/wordupload")
	public void wordUpload(MultipartFile wordFile,String wjjname,HttpServletResponse response){
		response.setContentType("text/html");
		String fileName = "";
		boolean flag = false;
		if(wordFile != null){
			if (!wordFile.isEmpty()) {
				String path = OSinfo.isWindows() ? CommonConstant.w_shijuanPath :CommonConstant.shijuanPath;
				String tmp = wordFile.getOriginalFilename();
				tmp = tmp.substring(tmp.lastIndexOf('.'));
				fileName = CommonFunImpl.uuid32() + tmp;
				File targetFile = new File(path, fileName);
				if(!targetFile.exists()){  
		            targetFile.mkdirs();  
		        }  
				
				//保存  
		        try {  
		        	wordFile.transferTo(targetFile);  
		        	flag = true;
		        } catch (Exception e) {  
		            e.printStackTrace();  
		        } 
			}
		}
		String ret = (flag == true) ? "1":"0";
		JSONObject jb = new JSONObject();
		jb.put("ret", ret);
		jb.put("path", wjjname + "/" + fileName);
		ret = jb.toString();
		try {
			PrintWriter out = response.getWriter();
			out.print(ret);
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
